package com.dong.module.foodmenu.controller;

import com.dong.common.annotation.RequestMapping;
import com.dong.common.pojo.ResultVO;
import com.dong.module.foodmenu.pojo.FoodMenu;
import com.dong.module.foodmenu.service.FoodMenuService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: 陈瑞东
 * @create: 2021-08-25 11:14
 */
@Controller
@RequestMapping("food")
public class FoodMenuController{

    @Autowired
    private FoodMenuService foodMenuService;


    @RequestMapping("save")
    public ResultVO<Void> saveFood(String id,String name, String filename) {
        try {
            foodMenuService.save(id, name, filename);
        } catch (Exception e) {
            return ResultVO.fail(e.getMessage());
        }
        return ResultVO.success(null);
    }

    @RequestMapping("saveDetail")
    public ResultVO<Void> saveDetail(String id, String ingredients, String practice) {
        try {
            foodMenuService.saveDetail(id, ingredients, practice);
        } catch (Exception e) {
            return ResultVO.fail(e.getMessage());
        }
        return ResultVO.success(null);
    }

    @RequestMapping("list")
    public ResultVO<List<FoodMenu>> listFood(int current, int size, String name) {
        List<FoodMenu> data = new ArrayList<>();
        try {
            data = foodMenuService.listFood(current, size, name);
        } catch (Exception e) {
            return ResultVO.fail(e.getMessage());
        }
        return ResultVO.success(data);
    }

    @RequestMapping("delete")
    public ResultVO<Void> delete(String id) {
        try {
            FoodMenu foodMenu = new FoodMenu();
            foodMenu.setId(id);
            foodMenuService.removeById(foodMenu);
        } catch (Exception e) {
            return ResultVO.fail(e.getMessage());
        }
        return ResultVO.success(null);
    }

    @RequestMapping("get")
    public ResultVO<FoodMenu> get(String id) {
        try {
            FoodMenu data = foodMenuService.getById(id);
            if (data != null) {
                return ResultVO.success(data);
            } else {
                return ResultVO.fail("菜单不存在");
            }
        } catch (Exception e) {
            return ResultVO.fail(e.getMessage());
        }
    }
}
